//
//  InternationalView.m
//  iFlower
//
//  Created by khanh nguyen on 7/30/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "InternationalView.h"
#import "ProductView.h"
#import "Country.h"
#import "Request.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "LoginView.h"
#import "RegisterView.h"

@implementation InternationalView
@synthesize lblCountry;
@synthesize countries;
@synthesize country;
-(IBAction)button_Menu:(id)sender;
{
	[self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)button_Next:(id)sender
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = self.country.categoryId;
	product.titleText = self.country.countryName;	
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
	
}
-(IBAction) buttonRegister
{
	RegisterView * registerView = [[RegisterView alloc] initWithNibName:@"RegisterView" bundle:nil];
	[self.navigationController pushViewController:registerView animated:YES];
	[registerView release];
}


-(IBAction)buttonSelec_Country:(id)sender
{
	actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
	[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
	CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
	pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
	pickerView.showsSelectionIndicator = YES;
	pickerView.dataSource = self;
	pickerView.delegate = self;
	[actionSheet addSubview:pickerView];
	//---
	UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
	closeButton.momentary = YES; 
	closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
	closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
	closeButton.tintColor = [UIColor blackColor];
	[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
	[actionSheet addSubview:closeButton];
	[closeButton release];
	
	[actionSheet showInView:self.view];
	
	[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
	
}
-(void)dismissActionSheet:(id)sender
{
	[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
	self.country = (Country*)[self.countries objectAtIndex:[pickerView selectedRowInComponent:0]];
	self.lblCountry.text = self.country.countryName;	
}


- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	NSMutableArray* data = [[NSMutableArray alloc] initWithCapacity:[[[Request sharedRequest] entries] count]];
	for (NSDictionary* dictionary in [[Request sharedRequest] entries])
	{
		Country* entry = [[Country new] autorelease];
		if (entry != nil)
		{
			entry.categoryId = [[dictionary objectForKey: @"categoryid"] intValue];
			entry.countryName = [dictionary objectForKey: @"category"];
			[data addObject: entry];
		}
	}
	NSSortDescriptor* descriptor = [[[NSSortDescriptor alloc] initWithKey:@"countryName"
																ascending:YES] autorelease];
	
	NSArray* sortDescriptors = [NSArray arrayWithObject:descriptor];
	self.countries = [data sortedArrayUsingDescriptors:sortDescriptors];	
	if([self.countries count]>0)
	{
		self.country = (Country*)[self.countries objectAtIndex:0];
		self.lblCountry.text = self.country.countryName;	
	}
	[data release];
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];
	[appDelegate hideLoadingView];
	[pickerView reloadAllComponents];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil ];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil ];
}
- (void) contentFailed:(NSNotification*) notification
{
	NSLog(@"content failed");
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];
	[appDelegate hideLoadingView];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil ];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil ];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];

    [appDelegate showLoadingView];
	
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
	
	[[Request sharedRequest] getCountryList];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[self.country release];
	[self.lblCountry release];
	[self.countries release];
    [super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
	
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
	
	return [self.countries count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	
	return [(Country*)[self.countries objectAtIndex:row] countryName];
}


@end
